﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using UIDemo.Properties;

namespace UIDemo
{
    public partial class DotNetForm : Form
    {
        public DotNetForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            InitTreeView();

            this.tabControl1.SelectedIndexChanged += TabControl1_SelectedIndexChanged;
        }

        private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        #region TreeView

        private TreeViewDragger _dragger;
        private Dictionary<string, Tuple<Type, Image>> _imgDic = new Dictionary<string, Tuple<Type, Image>>();

        private List<Models.Data> Datas = new List<Models.Data>()
        {
            new Models.DownData(){Name="001",
                SubNodes = new List<Models.Data>()
                {
                new Models.DownData(){Name="Sub_001"},
                new Models.DownData(){Name="Sub_002"},
                new Models.DownData(){Name="Sub_003"},
                new Models.DownData(){Name="Sub_004"},
                } },
            new Models.FoldData(){Name="002",
                SubNodes = new List<Models.Data>()
                {
                new Models.FoldData(){Name="Sub_001"},
                new Models.FoldData(){Name="Sub_002"},
                new Models.FoldData(){Name="Sub_003"},
                new Models.DownData(){Name="Sub_004"},
                } },
            new Models.DownData(){Name="003",
                SubNodes = new List<Models.Data>()
                {
                new Models.DownData(){Name="Sub_001"},
                new Models.FoldData(){Name="Sub_002"},
                new Models.DownData(){Name="Sub_003"},
                new Models.FoldData(){Name="Sub_004"},
                } },
            new Models.MuscData(){Name="004",
                SubNodes = new List<Models.Data>()
                {
                new Models.DownData(){Name="Sub_001"},
                new Models.MuscData(){Name="Sub_002"},
                new Models.MuscData(){Name="Sub_003"},
                new Models.DownData(){Name="Sub_004"},
                } },
            new Models.TickData(){Name="005",
                SubNodes = new List<Models.Data>()
                {
                new Models.DownData(){Name="Sub_001"},
                new Models.TickData(){Name="Sub_002"},
                new Models.DownData(){Name="Sub_003"},
                new Models.TickData(){Name="Sub_004"},
                } },
            new Models.MoveData(){Name="006",
                SubNodes = new List<Models.Data>()
                {
                new Models.MoveData(){Name="Sub_001"},
                new Models.DownData(){Name="Sub_002"},
                new Models.DownData(){Name="Sub_003"},
                new Models.MoveData(){Name="Sub_004"},
                } },
            new Models.StarData(){Name="007",
                SubNodes = new List<Models.Data>()
                {
                new Models.StarData(){Name="Sub_001"},
                new Models.DownData(){Name="Sub_002"},
                new Models.StarData(){Name="Sub_003"},
                new Models.DownData(){Name="Sub_004"},
                } },
            new Models.TickData(){Name="008",
                SubNodes = new List<Models.Data>()
                {
                new Models.DownData(){Name="Sub_001"},
                new Models.DownData(){Name="Sub_002"},
                new Models.DownData(){Name="Sub_003"},
                new Models.TickData(){Name="Sub_004"},
                } },
        };

        private void InitDrag()
        {
            _dragger = new TreeViewDragger(this.treeView1);
            _dragger.Func_CanDrag = n =>
            {
                return !(n.Tag is Models.StarData);
            };
            _dragger.Func_DoDrag = (sNode, dNode, b) =>
            {
                if (sNode == null || dNode == null)
                    return false;
                var sData = sNode.Tag as Models.Data;
                var dData = dNode.Tag as Models.Data;
                if (sNode.Parent == null)
                {
                    if (Datas.Contains(sData))
                        Datas.Remove(sData);
                }
                else
                {
                    var pnode = sNode.Parent.Tag as Models.Data;
                    if (pnode.SubNodes.Contains(sData))
                        pnode.SubNodes.Remove(sData);
                }
                if (b)
                {
                    dData.SubNodes.Insert(0, sData);
                }
                else
                {
                    if (dNode.Parent == null)
                        Datas.Insert(Datas.IndexOf(dData) + 1, sData);
                    else
                    {
                        var pdNode = dNode.Parent.Tag as Models.Data;
                        pdNode.SubNodes.Insert(pdNode.SubNodes.IndexOf(dData) + 1, sData);
                    }
                }
                return true;
            };
        }

        private void InitTreeView()
        {
            var ty = typeof(Models.DownData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.down16));

            ty = typeof(Models.FoldData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.folder16));

            ty = typeof(Models.MoveData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.movie16));

            ty = typeof(Models.MuscData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.music16));

            ty = typeof(Models.StarData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.star16));

            ty = typeof(Models.TickData);
            _imgDic.Add(ty.Name, new Tuple<Type, Image>(ty, Resources.tick16));

            var list = new ImageList();
            list.ImageSize = new Size(16, 16);
            foreach (var item in _imgDic)
            {
                Utils.ImageUtil.AddImageToCollection(item.Key, list, item.Value.Item2);
            }
            this.treeView1.ImageList = list;
            InitDrag();
            LoadData();

            this.treeView1.ItemHeight += 3 * 2;
        }

        private void LoadData()
        {
            this.treeView1.Nodes.Clear();
            for (int i = 0; i < Datas.Count; i++)
            {
                this.treeView1.Nodes.Add(ToNode(Datas[i]));
            }
            this.treeView1.Refresh();
        }

        private TreeNode ToNode(Models.Data data)
        {
            if (data == null)
                return null;
            var node = new TreeNode()
            {
                Text = data.Name,
                ImageKey = data.GetType().Name,
                SelectedImageKey = data.GetType().Name,
                Tag = data,
            };
            if (data.SubNodes != null)
            {
                data.SubNodes.ForEach(n =>
                {
                    node.Nodes.Add(ToNode(n));
                });
            }
            return node;
        }

        #endregion TreeView
    }
}